'''
This script loads the mapping file which contains the information for picking the right label based on
the sentence number
The result of predictions will be store in the file output.txt
'''
import pickle
import sys
import re

num_of_parameters = len(sys.argv)
if (num_of_parameters < 3 or num_of_parameters > 3):
    print "usage: python predict.py test_file output"
    sys.exit(0)

map_file = open("stats.map","r")
test_file = open(sys.argv[1],"r")
o = open(sys.argv[2],"w")

mapVar = pickle.load(map_file)

label_name_list = ["population","intervention","background","outcome","study design","other"]
o.write(test_file.readline().strip()+","+"prediction"+"\n") #write the heading line
while True:
    line = test_file.readline()
    if not line:
        break
    sent_number = line.split(",")[2]
    test = re.sub('"','',line.split(",")[0]) 
    predict = mapVar[int(sent_number)] #sent_number is the key which maps the most occurring label at a particular line number
    if (predict == test):
        output = "yes"
    else:
        output = "no"
    o.write(line.strip()+","+output+"\n")
test_file.close()
o.close()
map_file.close()